package org.dromara.chat.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.dromara.chat.domain.bo.ChatConfigBo;
import org.dromara.chat.domain.vo.ChatConfigVo;
import org.dromara.chat.service.ChatConfigService;
import org.dromara.common.core.domain.R;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 聊天配置控制器
 *
 * @author ruoyi
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/chat/config")
public class ChatConfigController extends BaseController {

    private final ChatConfigService chatConfigService;

    /**
     * 查询聊天配置列表
     */
    @SaCheckPermission("chat:config:list")
    @GetMapping("/list")
    public TableDataInfo<ChatConfigVo> list(ChatConfigBo bo, PageQuery pageQuery) {
        return chatConfigService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出聊天配置列表
     */
    @SaCheckPermission("chat:config:export")
    @Log(title = "聊天配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ChatConfigBo bo, HttpServletResponse response) {
        List<ChatConfigVo> list = chatConfigService.queryList(bo);
        ExcelUtil.exportExcel(list, "聊天配置", ChatConfigVo.class, response);
    }

    /**
     * 获取聊天配置详细信息
     */
    @SaCheckPermission("chat:config:query")
    @GetMapping("/{id}")
    public R<ChatConfigVo> getInfo(@PathVariable Long id) {
        return R.ok(chatConfigService.queryById(id));
    }

    /**
     * 新增聊天配置
     */
    @SaCheckPermission("chat:config:add")
    @Log(title = "聊天配置", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated @RequestBody ChatConfigBo bo) {
        return toAjax(chatConfigService.insertByBo(bo));
    }

    /**
     * 修改聊天配置
     */
    @SaCheckPermission("chat:config:edit")
    @Log(title = "聊天配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated @RequestBody ChatConfigBo bo) {
        return toAjax(chatConfigService.updateByBo(bo));
    }

    /**
     * 删除聊天配置
     */
    @SaCheckPermission("chat:config:remove")
    @Log(title = "聊天配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@PathVariable Long[] ids) {
        return toAjax(chatConfigService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 根据配置分类查询配置列表
     */
    @SaCheckPermission("chat:config:query")
    @GetMapping("/category/{category}")
    public R<List<ChatConfigVo>> getByCategory(@PathVariable String category) {
        return R.ok(chatConfigService.queryListByCategory(category));
    }

    /**
     * 根据配置名称查询配置
     */
    @SaCheckPermission("chat:config:query")
    @GetMapping("/name/{configName}")
    public R<ChatConfigVo> getByConfigName(@PathVariable String configName) {
        return R.ok(chatConfigService.queryByConfigName(configName));
    }

}